const CryptoJS = require("crypto-js");
const MD5 = require("crypto-js/md5");

// AES密钥以及偏移量
const key = CryptoJS.enc.Utf8.parse("bfnXVavEJyUFelhc");
// const iv = CryptoJS.enc.Utf8.parse("0392039203920300");

// AES加密
const AESencrypt = (message) => {
  const ciphertext = CryptoJS.AES.encrypt(message, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  const hexString = ciphertext.ciphertext.toString(CryptoJS.enc.Hex);
  return hexString;
};

// AES解密
const AESdecrypt = (secret) => {
  let encryptedHexStr = CryptoJS.enc.Hex.parse(secret);
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  let decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr;
};

// MD5加密
const MD5encrypt = (message) => {
  let result;
  result = MD5(message);
  return result + "";
};

export { AESencrypt, AESdecrypt, MD5encrypt };
